
var _NoDel = false;
var _NoEdit = false;
 /**
 * show the mouse menu
 */
function __showMouseMenu() {
  var btn = event.button;
  if (btn==2) {
    __getMouseMenu().show();
  }else {
    if (window.STATICCLASS_MOUSEMENU!=null) {
      window.STATICCLASS_MOUSEMENU.hide();
    }
  }
}

/**
 * get the mouse menu instance
 */
function __getMouseMenu() {
  if (window.STATICCLASS_MOUSEMENU==null) {
    window.STATICCLASS_MOUSEMENU = new TreeMouseMenu();
  }
  return window.STATICCLASS_MOUSEMENU;
}


/**
 * fix the padding style
 */
function __pad($domObj, $iFix) {
  try {
    $domObj.style.padding = $domObj.currentStyle.padding.replace(/\d+/g, function($1){return $1*1+$iFix;});
  }catch(e) {
  }
}
/**
 * 在使用日程安排时，该类覆盖了默认的操作类
 * @author: TANTOM
 */
function TreeMouseMenu() {
	var _panel = null;
	var _panelNode = null;
	var _domItems = null;

	var _activeTD = null;
	/**
	 * create the panel html
	 */
	this.init = function init($dom) {
		_panel = new JPanel();
		_panelNode = _panel.getNode();
		var tmp = [];
   	tmp.push("<div style='border:solid 1px black;background-color:#ffffff' onmousedown='event.cancelBubble=true;'>");
    tmp.push("<table cellspacing=0 cellpadding=1 style='background-repeat: repeat-y;background-image: url(" + _c + _image + "/img/juice/menub.gif)'>");
    tmp.push("<tr><td><table cellspacing=0 cellpadding=1>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=root.org.add tt.order=0>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/add.gif>增加根单位</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=org.edit tt.order=1>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/edit.gif>修改单位</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=org.del tt.order=2>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/del.gif>删除单位</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=org.add tt.order=3>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/add.gif>增加子单位</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=dept.add tt.order=4>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/add.gif>增加部门</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=dept.edit tt.order=5>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/edit.gif>修改部门</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=dept.del tt.order=6>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/del.gif>删除部门</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=pos.add tt.order=7>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/add.gif>增加岗位</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=pos.edit tt.order=8>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/edit.gif>修改岗位</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=pos.del tt.order=9>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/del.gif>删除岗位</td></tr>");    

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=user.add tt.order=10>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/add.gif>增加用户</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=role.add tt.order=11>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/add.gif>增加角色</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=role.edit tt.order=12>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/edit.gif>修改角色资料</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=role.perm tt.order=13>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/edit.gif>修改角色权限</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=role.del tt.order=14>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/del.gif>删除角色</td></tr>");

    tmp.push("<tr><td nowrap style='padding:1 1 1 1;cursor:default;' TT.type=role.batch tt.order=15>");
    tmp.push("<img align=absmiddle style='margin-right:2px' src=" + _c + _image + "/img/add.gif>批量关联</td></tr>");

    tmp.push("</table></td></tr>");
    tmp.push("</table></div>");

		_panelNode.innerHTML = tmp.join("");

    var domTable = _panelNode.children[0].children[0].rows[0].cells[0].children[0];
    var domTrs = domTable.rows;
    var len = domTrs.length;
    for (var i=0; i<len; i++) {
      domTrs[i].children[0].attachEvent("onmouseover", active);
      domTrs[i].children[0].attachEvent("onmouseout", sleep);
      domTrs[i].children[0].attachEvent("onmousedown", click);
    }
    _domItems = domTrs;
	};

  /**
   * unactive a menu style
   */
  function active() {
    var domObj = window.event.srcElement;
    domObj = __getReal(domObj, "tagName", "TD");
    if (domObj==null)
      return;
		if (domObj.getAttribute("tt.type")==null)
			return;

    var s = domObj.style;
    s.border = "1px solid black";
    __pad(domObj, -1);
    s.backgroundColor = "#ADB9D8";
  }

  /**
   * active the menu style
   */
  function sleep() {
    var domObj = window.event.srcElement;
    domObj = __getReal(domObj, "tagName", "TD");
    if (domObj==null)
      return;

    var s = domObj.style;
    s.borderStyle = "none";
    __pad(domObj, 1);
    s.backgroundColor = "transparent";
  }

  /**
   * click an menu
   */
  function click() {
    var dom = window.event.srcElement;
    dom = __getReal(dom, "tagName", "TD");

    var type = dom.getAttribute("tt.type");
    if (type==null)
      return;

    var ppos = __getPos(_panelNode);    
    var fix = [];
    fix.x = ppos.x;
    fix.y = ppos.y;

    /** 如果在下面会造成Select的显示与消失的冲突 */
    _panel.setVis(false);
    __invokeAction(type, fix); //调用外部的处理方法
  }

  /**
   * hide the indexed item menu
   */
	function hideItem($index) {
		_domItems[$index].style.display = "none";
	}

	function hideAll() {
	  for (var i=0; i<_domItems.length; i++) {
	    _domItems[i].style.display = "none";
	  }
	}

	/**
	 * show the indexed item menu
	 */
	function showItem($index) {
	  if ($index.indexOf(",")!=-1) {
	    var s = $index.split(",");
	    for (var i=0; i<s.length; i++) {
        _domItems[s[i]].style.display = "";
	    }
	  }else {
		  _domItems[$index].style.display = "";
		}
	}

  /**
   * force the menu to hide
   */
  function forceHide() {
    if (_panel!=null) {
      _panel.setVis(false);
      hideSelectedDiv();
    }
  }

	/**
	 * show the mouse menu
	 */
	this.show = show;

	function show() {
	  var domDown = window.event.srcElement;
	  var domTr = __getReal(domDown, "className", "nodeName");

    if (_panel==null) {
			init();
		}


	  if (domTr==null) {
	    forceHide();
	    return;
	  }

	  /**
	   * first call the tree to invoke the select event
	   */
	   var n = domTr.getAttribute("id");
     var nodeKey = n.replace("ATreeNAME", "");
     ATree.clickNode(nodeKey, true);
     var nodeModel = ATree.getNodeModel(nodeKey);

     hideAll();
     if (nodeModel.url=="O") { //组织单位
       showItem("1,2,3,4,10");
       showPanel();
     }else if (nodeModel.url=="D") { //部门
       showItem("5,6,7,10");
       showPanel();
     }else if (nodeModel.url=="P") { //岗位
       showItem("8,9,10");
       showPanel();
     }else if (nodeModel.url=="R") { //根节点
       showItem("0");
       showPanel();
     }else if (nodeModel.url=="Role") { //角色
       var k = nodeModel.key;
       if (k=="Role@Tree") { //角色根节点
         return;
       }
       if (k.indexOf("@@")==-1) { //角色组
         //showItem("11");
         //showPanel();
       }else { //角色
         showItem("12,13,14,15");
         showPanel();
       }
     }
	};

	function showPanel() {
	  _panelNode.style.left = window.event.x + document.body.scrollLeft;
      _panelNode.style.top = window.event.y + document.body.scrollTop;
      _panel.setVis(true);
      //修正控制面板所在的位置     
      var totalHeight = _panelNode.clientHeight + window.event.y;
      if (totalHeight+10>document.body.clientHeight) {
        _panelNode.style.top = window.event.y - _panelNode.clientHeight;  
      }      
    }

	this.hide = hide;

	function hide() {
	   if (_panel!=null) {
		 _panel.setVis(false);
		 hideSelectedDiv();
		}
	};

	function hideSelectedDiv() {

	}
}

function MMOnLoad() {
  document.body.oncontextmenu = new Function("return false");
  document.body.attachEvent("onmousedown", __showMouseMenu);
}

window.attachEvent("onload", MMOnLoad);


/**
 * 获得触发事件的坐标
 */
function __getEventPos() {
  var x = window.event.x + document.body.scrollLeft;
  var y = window.event.y + document.body.scrollTop;
  return {'x':x,'y':y};
}